//
// LibGraphic_ncurse.cpp for nibbler in /home/moreau_r//tek2/c++/nibbler/nibbler-2016-gomes_m/lib
// 
// Made by tristan moreau
// Login   <moreau_r@epitech.net>
// 
// Started on  Sun Mar 24 10:23:08 2013 tristan moreau
// Last update Fri Jul 26 17:37:59 2013 mickael gomes
//

#include		"IGraphicLibrary.hh"
#include		"LibGraphic_ncurse.hh"

LibGraphic_Ncurse::PictureOfElement::PictureOfElement(char c, IGraphicLibrary::e_ListOfImage type)
  : m_char(c), m_type(type)
{

}

bool		LibGraphic_Ncurse::PictureOfElement::isThatType(IGraphicLibrary::e_ListOfImage type) const
{
  return (m_type == type);
}

char		LibGraphic_Ncurse::PictureOfElement::getChar(void) const
{
  return (m_char);
}

IGraphicLibrary::e_ListOfImage	LibGraphic_Ncurse::PictureOfElement::getType(void) const
{
  return (m_type);
}

LibGraphic_Ncurse::LibGraphic_Ncurse(unsigned int const size_x, unsigned int const size_y, std::vector<IGraphicLibrary::InfoPicture> const & listOfElement)
  : m_size_x(size_x), m_size_y(size_y)
{
  for (unsigned int i = 0; i < listOfElement.size(); i++)
    {
      m_pictures.push_back(PictureOfElement(listOfElement[i].getChar(), listOfElement[i].getType()));
    }

  initscr();
  clear();
  noecho();
  nodelay(stdscr,true);
  keypad(stdscr,true);
  cbreak(); 
}

void			LibGraphic_Ncurse::refreshScreen(void)
{
  refresh();
}

char			LibGraphic_Ncurse::find_carac(IGraphicLibrary::e_ListOfImage e)
{
  for (unsigned int i = 0; i < m_pictures.size(); i++)
    {
      if (m_pictures[i].getType() == e)
	return m_pictures[i].getChar();
    }
  return (-1);
}

void			LibGraphic_Ncurse::draw(unsigned int x, unsigned int y, IGraphicLibrary::e_ListOfImage const e, IGraphicLibrary::e_Direction const dir)
{
  char			carac;

  carac = find_carac(e);
  if (carac == -1)
    return ;
  mvaddch(y, x, carac);
}

IGraphicLibrary::e_TypeOfEvent	LibGraphic_Ncurse::checkEvent(void)
{
  int			key;

  key = getch();
  switch (key)
    {
    case KEY_UP:
      return (IGraphicLibrary::KB_UP);
    case KEY_DOWN:
      return (IGraphicLibrary::KB_DOWN);
    case KEY_LEFT:
      return (IGraphicLibrary::KB_LEFT);
    case KEY_RIGHT:
      return (IGraphicLibrary::KB_RIGHT);
    case 'p':
      return (IGraphicLibrary::PAUSE);
    case 27:
      return (IGraphicLibrary::EXIT);
    }
  return (IGraphicLibrary::NONE);
}

void			LibGraphic_Ncurse::draw_footer(unsigned int x, unsigned int y, std::string const & str)
{
  mvprintw(y, x, str.c_str());
  mvaddch(y + 1, x, 10);
}

extern "C"
{
  IGraphicLibrary	*make_IGraphicLibrary(unsigned int size_x, unsigned int size_y, std::vector<IGraphicLibrary::InfoPicture> const & list)
  {
    return (new LibGraphic_Ncurse(size_x, size_y, list));
  }
}
